package com.flowerplus.store;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.flowerplus.store.app.Navigation;
import com.flowerplus.store.app.RxDisposable;
import com.flowerplus.store.app.photo.model.ImageItem;
import com.flowerplus.store.common.data.injector.HasComponent;
import com.flowerplus.store.common.data.utils.FileUtil;
import com.flowerplus.store.common.data.utils.glide.GlideUtil;
import com.flowerplus.store.common.data.utils.ui.BottomSheetUtil;
import com.flowerplus.store.common.structure.PermissionManager;
import com.flowerplus.store.common.structure.PhotoManager;
import com.flowerplus.store.common.structure.mvp.BasePresenter;
import com.flowerplus.store.common.structure.presenter.photo.PhotoContract;
import com.flowerplus.store.common.structure.presenter.photo.PhotoPresenter;
import com.flowerplus.store.common.structure.ui.activity.AppToolbarActivity;
import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;
import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

import static com.flowerplus.store.common.structure.PhotoManager.REQUEST_CODE_OPEN_CAMERA;

public class MainActivity extends AppToolbarActivity implements  PhotoContract.View,HasComponent<MainActivityCompent> {

    @BindView(R.id.btn_up)
    Button mBtnUp;
    @BindView(R.id.iv_photo)
    ImageView mIvPhoto;
    @Inject
    PhotoPresenter mPhotoPresenter;
    @Inject
    PermissionManager mPermissionManager;
    @Inject
    RxDisposable mRxDisposable;
    @Inject
    FileUtil mFileUtil;

    private BottomSheetDialog mPhotoDialog;
    private View mDialogView;
    private Button mButCancel;
    private Button mButOpen;
    private Button mButPhoto;
    private File mFile;

    MainActivityCompent mCircularComponent;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

    }

    @Override
    protected void initViews() {
        mPhotoPresenter.onCreate(this);
        mPermissionManager.setRxPermissions(new RxPermissions(this));
        mDialogView = LayoutInflater.from(this).inflate(R.layout.layout_dialog_photo, null);
        mButCancel = mDialogView.findViewById(R.id.but_cancel);
        mButOpen = mDialogView.findViewById(R.id.but_open);
        mButPhoto = mDialogView.findViewById(R.id.but_photo);

        mPhotoDialog = new BottomSheetDialog(this);
        mPhotoDialog.setContentView(mDialogView);
        BottomSheetUtil.statusBarTranslucent(mPhotoDialog);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {

        mButPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPhotoDialog.dismiss();
//                Navigation.goPhotoLoaderActivity(MainActivity.this, PhotoManager.REQUEST_CODE_CHECK_IMAGE, 0);
                mRxDisposable.add(mPermissionManager.checkPermissionShowNick(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        Navigation.goPhotoLoaderActivity(mActivity, PhotoManager.REQUEST_CODE_CHECK_IMAGE, 5);
                    }
                }, "存储卡", Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE));
            }
        });
        mButCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPhotoDialog.dismiss();
            }
        });
        //打开相机
        mButOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPhotoDialog.dismiss();
                //打开相机
                mRxDisposable.add(mPermissionManager.checkPermissionShowNick(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        mFile = new File(mFileUtil.getRootDir(), System.currentTimeMillis() + ".jpg");
                        //打开相机
                        PhotoManager.openCamera(MainActivity.this, mFile, REQUEST_CODE_OPEN_CAMERA);
                    }
                }, "相机", Manifest.permission.CAMERA));
            }
        });
        mBtnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                BottomSheetUtil.collapsedDialog(mDialogView);
//                mPhotoDialog.show();
                Navigation.goLoginModeActivity(mActivity);

            }
        });
    }

    @Override
    public void initInject() {
       mCircularComponent =DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build();
        mCircularComponent.inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void compressComplete(File file) {
        GlideUtil.load(this,file,mIvPhoto);
    }

    @Override
    public void uploadComplete(String path) {

    }

    @Override
    public void uploadCompleteThumb(String path) {

    }

    @Override
    public void uploadError(String msg) {

    }

    @Override
    public void photoCallback(String path) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {


                case PhotoManager.REQUEST_CODE_CHECK_IMAGE:
                    //选择图片回调 并裁剪
                    ArrayList<ImageItem> path = data.getParcelableArrayListExtra("path");
                    mFile = new File(mFileUtil.getHeaderDir().getPath(), System.currentTimeMillis() + ".jpg");
                    PhotoManager.crop(this, mFile, new File(path.get(0).getImagePath()), 960, 600, PhotoManager.REQUEST_CODE_CROP_CODE);
                    break;
                case REQUEST_CODE_OPEN_CAMERA:
                    //相机回调
                    PhotoManager.crop(this, mFile, mFile,960,600, PhotoManager.REQUEST_CODE_CROP_CODE);
                    break;
                case PhotoManager.REQUEST_CODE_CROP_CODE:
                    //裁剪回调
                    //压缩
                    mPhotoPresenter.compressPhoto(mFile);
                    break;


            }
        }
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    public MainActivityCompent getComponent() {
        return mCircularComponent;
    }
}
